import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  SmsSignApi,
  SmsSignReq,
  SmsSignModel,
  SmsSignPage,
  SmsSignList,
  SmsSignKey,
  SmsSignKeyList,
  SmsSignMoveKey,
} from "./types";
import type { SmsAppConfigKey } from '../sms-app-config/types';

class SmsSignApiImpl implements SmsSignApi {
  getKey(model: SmsSignModel): SmsSignKey {
    return {
      signId: model.signId,
    } as SmsSignKey;
  }

  // 查询翻页信息短信签名
  findPage(query?: SmsSignReq): AxiosPromise<SmsSignPage> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_sign/page/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询列表信息短信签名
  findList(query?: SmsSignReq): AxiosPromise<SmsSignList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_sign/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询短信签名
  findById(query: SmsSignKey): AxiosPromise<SmsSignModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_sign/id/" + tenant + "/" + query.signId,
      method: "get",
    });
  }

  // 上移下移
  move(key: SmsSignKey, up: boolean): AxiosPromise<boolean> {
    const data: SmsSignMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/sms/sms_sign/move",
      method: "put",
      data: data,
    });
  }

  // 删除短信签名
  delete(query: SmsSignKey): AxiosPromise<SmsSignModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_sign/id",
      method: "delete",
      params: query,
    });
  }

  // 批量删除短信签名
  batchDelete(keys: SmsSignKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.signId);
    const data = {
      tenant: useUserStore().user.tenant,
      signId: ids,
    } as SmsSignKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/sms/sms_sign/batch",
      method: "put",
      data: data,
    });
  }
  // 同步短信签名
  sync(query?: SmsAppConfigKey): AxiosPromise<boolean> {
   const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/sms/sms_sign/sync/" + tenant,
      method: "get",
      params: query,
    });
  }
}
const smsSignApi = new SmsSignApiImpl();

export { smsSignApi };
